//
//  WelcomViewController.swift
//  WKweiBo
//
//  Created by 阿拉斯加的狗 on 15/12/10.
//  Copyright © 2015年 阿拉斯加的🐶. All rights reserved.
//

import UIKit
import SDWebImage
class WelcomViewController: UIViewController {

    //设置view
    override func loadView() {
        view = backImageView
        
        //更新UI控件
        setUpUI()
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //异步加载图片
        iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.userAvatarUrl, placeholderImage: UIImage(named: "avatar_default_big"))
    }

/// 显示界面
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 200)
        }
        welcomLabel.alpha = 0
        
        UIView.animateWithDuration(1.2,
            delay: 0,
            usingSpringWithDamping: 0.8,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                
                self.view.layoutIfNeeded()
                
            }) { _ in
                
                UIView.animateWithDuration(0.8, animations: {
                    
                    self.welcomLabel.alpha = 1.0
                    
                    }) { _ in
                
                    //发送通知
                    NSNotificationCenter.defaultCenter().postNotificationName(WKSwitchRootViewControllerNotification, object: nil)
                
                        
                }
        }
    }
    
 /// 设置界面
    private func setUpUI(){
    
        view.addSubview(iconView)
        view.addSubview(welcomLabel)
    
        //设置自动布局
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
    
        welcomLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
    }
    

/// 懒加载控件
    private lazy var backImageView: UIImageView = UIImageView (imageName: "ad_background")
    
/// 用户头像
    private lazy var iconView: UIImageView = {
    
        let iv = UIImageView(imageName: "avatar_default_big")
        
        //设置圆角
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        
        return iv
    
    }()
    
/// 设置标签
    private lazy var welcomLabel: UILabel = UILabel(title: "欢迎归来",  fontSize: 18)

}
